package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.dto.BlackListDTO;
import org.example.mapper.BlackListMapper;
import org.example.model.BlackListDO;
import org.example.req.BlackListDelReq;
import org.example.req.BlackListSaveReq;
import org.example.req.BlackListUpdateReq;
import org.example.service.BlackListService;
import org.example.utils.SpringBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlackListServiceImpl implements BlackListService {

    @Resource
    private BlackListMapper blackListMapper;
    @Override
    public List<BlackListDTO> list(String result) {
        if (result == null){
            List<BlackListDO> blackListDOS = blackListMapper.selectList(null);
            List<BlackListDTO> blackListDTOS = SpringBeanUtil.copyProperties(blackListDOS, BlackListDTO.class);
            return blackListDTOS;
        }
        LambdaQueryWrapper<BlackListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackListDO::getResult, result);
        List<BlackListDO> blackListDOS = blackListMapper.selectList(queryWrapper);

        return SpringBeanUtil.copyProperties(blackListDOS, BlackListDTO.class);
    }

    @Override
    public BlackListDTO findById(Long id) {
        LambdaQueryWrapper<BlackListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackListDO::getId, id);
        BlackListDO blackListDO = blackListMapper.selectOne(queryWrapper);
        return SpringBeanUtil.copyProperties(blackListDO, BlackListDTO.class);
    }

    @Override
    public int save(BlackListSaveReq blackListSaveReq) {
        BlackListDO blackListDO = SpringBeanUtil.copyProperties(blackListSaveReq, BlackListDO.class);

        return blackListMapper.insert(blackListDO);
    }

    @Override
    public int delete(BlackListDelReq blackListDelReq) {
        if (blackListDelReq.getId() != null){
            return blackListMapper.deleteById(blackListDelReq.getId());
        }
        return 0;
    }

    @Override
    public int update(BlackListUpdateReq blackListUpdateReq) {
        LambdaQueryWrapper<BlackListDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackListDO::getId, blackListUpdateReq.getId());
        int update = blackListMapper.update(SpringBeanUtil.copyProperties(blackListUpdateReq, BlackListDO.class), queryWrapper);
        return update;
    }
}
